<template>
  <view>
    <!-- 模块1：收货地址 -->
    <view class="address" @click="getAddress">
      <!-- 正常情况：显示收货地址 -->
      <view v-if="address.userName" class="address-info">
        <view class="address-detail">{{ detailAddress }}</view>
        <view class="address-user">
          {{ address.userName }} {{ address.telNumber }}
        </view>
      </view>
      <!-- 互斥情况：显示添加按钮 -->
      <view v-else class="address-btn">添加地址</view>
    </view>
    <!-- 模块2：已选商品列表 -->
    <view class="goods-list">
      <view class="goods-title">已选商品</view>
      <view
        class="cart-item"
        v-for="item in selectedCartList"
        :key="item.goods_id"
      >
        <view class="cart-count"> x{{ item.goods_count }} </view>
        <navigator
          class="goods-item"
          hover-class="none"
          :url="`/pages/goods_detail/index?goods_id=${item.goods_id}`"
        >
          <!-- 商品图片 -->
          <image
            class="goods-image"
            :src="item.goods_small_logo"
            mode="aspectFit"
          />
          <!-- 商品信息 -->
          <view class="goods-info">
            <view class="goods-name">{{ item.goods_name }}</view>
            <view class="goods-price">{{ item.goods_price }}</view>
          </view>
        </navigator>
      </view>
    </view>
    <!-- 模块3：底部操作栏 -->
    <view class="bottom-bar">
      <view class="bottom-bar-fixed">
        <!-- 合计 -->
        <view class="total">
          <text class="total-name">合计：</text>
          <text class="total-price">{{ selectedCartListPrice }}</text>
        </view>
        <!-- 结算 -->
        <view class="account" @click="goPay">
          去支付({{ selectedCartListCount }})
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      //
      address: {},
    };
  },
  computed: {
    ...mapGetters([
      "selectedCartList",
      "selectedCartListCount",
      "selectedCartListPrice",
    ]),
    detailAddress() {
      const { provinceName, cityName, countyName, detailInfo } = this.address;
      return [provinceName, cityName, countyName, detailInfo].join(" ");
    },
  },
  methods: {
    async getAddress() {
        // #ifdef H5
      alert("暂不支持");
      // #endif
      // #ifndef H5
      const [error, res] = await uni.chooseAddress();
      console.log(res);
      this.address = res;
       // #endif
    },
    // 去支付
    async goPay() {
      // 判断有没有收获地址
      // 有
      if (this.detailAddress.trim()) {
        // 创建订单获取订单编号
        const data = {
          order_price: this.selectedCartListPrice,
          consignee_addr: this.detailAddress,
          goods: this.selectedCartList.map((item) => {
            return {
              goods_id: item.goods_id,
              goods_number: item.goods_count,
              goods_price: item.goods_price,
            };
          }),
        };
        const res = await uni.$u.http.post("/my/orders/create", data);
        // console.log(res);
        const { order_number } = res.data.message;
        // 根据订单编号获取支付参数
        const res2 = await uni.$u.http.post("/my/orders/req_unifiedorder", {
          order_number,
        });
        // console.log(res2);
        const { pay } = res2.data.message;
        // 调用支付api进行支付
        const [error, res3] = await uni.requestPayment(pay);
        console.log(res3);
      } else {
        return uni.showToast({
          title: "请选择收获地址~",
          icon: "none",
        });
      }
    },
  },
};
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}
// 模块1：收货地址
.address {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .address-info {
    width: 100%;
    .address-detail {
      font-size: 30rpx;
      font-weight: bold;
      margin-bottom: 20rpx;
    }
  }
  .address-btn {
    width: 303rpx;
    height: 90rpx;
    border-radius: 45rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #eb4450;
    color: #eb4450;
  }
}

// 模块2：已选商品列表
.goods-list {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;
  .goods-title {
    line-height: 40rpx;
    font-size: 26rpx;
  }
  .cart-item {
    position: relative;
    .cart-count {
      position: absolute;
      right: 20rpx;
      bottom: 30rpx;
    }
    // 选中最后一项
    &:last-child {
      .goods-item {
        border: none;
      }
    }
  }
}
// 全局公共样式
.goods-list {
  .goods-item {
    display: flex;
    border-bottom: 1rpx solid #ddd;
    padding: 25rpx;
    .goods-image {
      width: 190rpx;
      height: 190rpx;
    }
    .goods-info {
      flex: 1;
      font-size: 24rpx;
      // 垂直方向两端对齐
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx;
      .goods-name {
        // 多行超出隐藏
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .goods-price {
        color: #eb4450;
        // 前面的价格符号
        &::before {
          content: "￥";
          font-size: 80%;
          margin-right: 5rpx;
        }
      }
    }
  }
}

// 底部操作栏
.bottom-bar {
  // 占位用
  height: 100rpx;
  // 定位的操作栏
  .bottom-bar-fixed {
    height: 80rpx;
    position: fixed;
    z-index: 99;
    left: 0;
    right: 0;
    bottom: 0;

    border-top: 1px solid #eee;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 20rpx;

    .select {
      .select-radio {
        font-size: 24rpx;
        color: #999;
      }
    }

    .total {
      flex: 1;
      display: flex;
      align-items: center;
      margin-left: 40rpx;
      .total-name {
        font-size: 24rpx;
      }

      .total-price {
        color: #eb4450;
      }
    }

    .account {
      background-color: #eb4450;
      color: #fff;
      font-size: 22rpx;
      width: 150rpx;
      height: 52rpx;
      border-radius: 26rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>


